package com.siyu.tickets.dao.impl;

import java.util.Calendar;
import java.util.Date;

import org.springframework.stereotype.Repository;

import com.siyu.common.hibernate3.Finder;
import com.siyu.common.hibernate3.HibernateBaseDao;
import com.siyu.common.page.Pagination;
import com.siyu.tickets.dao.TicketVerifyDao;
import com.siyu.tickets.entity.TicketVerify;

@Repository
public class TicketVerifyDaoImpl extends HibernateBaseDao<TicketVerify, Integer> implements TicketVerifyDao {

	public Pagination getPage(Integer supplierId, Date beginTime, Date endTime, int pageNo, int pageSize) {
		Finder f = Finder.create("select bean from TicketVerify bean");
		f.append(" where 1=1");
		if (supplierId != null) {
			f.append(" and bean.supplier.id = :supplierId");
			f.setParam("supplierId", supplierId);
		}
		if (beginTime != null) {
			f.append(" and bean.createTime >= :beginTime");
			f.setParam("beginTime", beginTime);
		}
		if (endTime != null) {// 由于传入的传入的日期不带时间，所以这儿要做日期+1处理
			Calendar cal = Calendar.getInstance();
			cal.setTime(endTime);
			cal.add(Calendar.DAY_OF_YEAR, 1);
			endTime = cal.getTime();
			f.append(" and bean.createTime <= :endTime");
			f.setParam("endTime", endTime);
		}
		f.append(" order by bean.createTime desc");
		return find(f, pageNo, pageSize);
	}

	public TicketVerify save(TicketVerify bean) {
		getSession().save(bean);
		return bean;
	}

	public TicketVerify deleteById(Integer id) {
		TicketVerify entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	protected Class<TicketVerify> getEntityClass() {
		return TicketVerify.class;
	}

}
